package br.caixa2.reports;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import br.caixa2.entities.AccountImpl;
import br.caixa2.entities.CurrentAccount;
import br.caixa2.entities.SavingAccount;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class AccountListReport {
	private static Font fontTitulo = new Font( Font.HELVETICA, 16, Font.BOLDITALIC );

	public static void report( List<AccountImpl> list ) throws IOException {
		File file = File.createTempFile( "accountReport", ".pdf" );
		try {
			Document document = new Document();
			PdfWriter.getInstance( document, new FileOutputStream( file ) );
			document.open();
			addTitlePage( document, "Lista de Contas Corrente\n" );

			PdfPTable accCurrentTable = new PdfPTable( 4 );
			accCurrentTable.setWidthPercentage( 100 );
			accCurrentTable.addCell( new PdfPCell( new Paragraph( "Número" ) ) );
			accCurrentTable.addCell( new PdfPCell( new Paragraph( "Cliente" ) ) );
			accCurrentTable.addCell( new PdfPCell( new Paragraph( "Saldo" ) ) );
			accCurrentTable.addCell( new PdfPCell( new Paragraph( "Limite" ) ) );

			PdfPTable accSavingTable = new PdfPTable( 4 );
			accSavingTable.setWidthPercentage( 100 );
			accSavingTable.addCell( new PdfPCell( new Paragraph( "Número" ) ) );
			accSavingTable.addCell( new PdfPCell( new Paragraph( "Cliente" ) ) );
			accSavingTable.addCell( new PdfPCell( new Paragraph( "Saldo" ) ) );
			accSavingTable.addCell( new PdfPCell( new Paragraph( "Variação" ) ) );

			for ( AccountImpl accountImpl : list )
				if ( accountImpl instanceof CurrentAccount ) {
					accCurrentTable.addCell( new PdfPCell( new Paragraph( accountImpl.getNumber() + "" ) ) );
					accCurrentTable.addCell( new PdfPCell( new Paragraph( accountImpl.getClient().getName() ) ) );
					accCurrentTable.addCell( new PdfPCell( new Paragraph( String.format( "R$ %.2f", accountImpl.getBalance() ) ) ) );
					accCurrentTable.addCell( new PdfPCell( new Paragraph( ( (CurrentAccount) accountImpl ).getLimit() + "" ) ) );
				} else {
					accSavingTable.addCell( new PdfPCell( new Paragraph( accountImpl.getNumber() + "" ) ) );
					accSavingTable.addCell( new PdfPCell( new Paragraph( accountImpl.getClient().getName() ) ) );
					accSavingTable.addCell( new PdfPCell( new Paragraph( String.format( "R$ %.2f", accountImpl.getBalance() ) ) ) );
					accSavingTable.addCell( new PdfPCell( new Paragraph( ( (SavingAccount) accountImpl ).getVariation() + "" ) ) );
				}
			document.add( accCurrentTable );
			addTitlePage( document, "Lista de Contas Poupanças\n" );
			document.add( accSavingTable );
			document.close();
		} catch ( Exception e ) {
			e.printStackTrace();
		}

		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

		try {
			desktop.open( file );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
	}

	private static void addTitlePage( Document document, String title ) throws DocumentException {
		Paragraph preface = new Paragraph();
		preface.add( new Paragraph( title, fontTitulo ) );
		document.add( preface );
	}

}
